package org.example.lotterysystem.service.activitystatus.operater;

import jakarta.annotation.Resource;
import org.example.lotterysystem.common.domain.dto.ConvertActivityStatusDTO;
import org.example.lotterysystem.common.domain.enums.ActivityPrizeStatusEnum;
import org.example.lotterysystem.common.domain.enums.ActivityStatusEnum;
import org.example.lotterysystem.mapper.ActivityMapper;
import org.example.lotterysystem.mapper.ActivityPrizeMapper;
import org.example.lotterysystem.mapper.dataobject.ActivityDO;
import org.springframework.stereotype.Component;

@Component
public class ActivityOperator extends AbstractActivityOperator{

    @Resource
    ActivityMapper activityMapper;

    @Resource
    ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityStatusEnum targetActivityStatus = convertActivityStatusDTO.getTargetActivityStatus();
        if(activityId==null || targetActivityStatus==null){
            return false;
        }
        ActivityDO activityDO = activityMapper.selectById(activityId);
        if(activityDO==null){
            return false;
        }
        if(targetActivityStatus.name().equalsIgnoreCase(activityDO.getStatus())){
            return false;
        }
        int count=activityPrizeMapper.countPrize(activityId, ActivityPrizeStatusEnum.INIT.name());
        return count <= 0;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            activityMapper.updateStatus(convertActivityStatusDTO.getActivityId(),
                    convertActivityStatusDTO.getTargetActivityStatus().name());
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
